var uid=localStorage.getItem("username");
var goods_num=document.querySelector(".goods_num");
//details.html  购物车添加商品
if(uid){
    function addgoods(pid){
        /* 给购物车添加商品 */
        var uid=JSON.parse(localStorage.getItem("username"));
        var pnum=goods_num.innerHTML;
        axios.get(AddGoods,{params:{uid,pid,pnum}}).then(function(res){
        showMycar();
        })
    }
    
    //allgoods.html  购物车添加商品
    function addOne($this,pid){
        var uid=JSON.parse(localStorage.getItem("username"));
        $this.style.color="blue";
        axios.get(AddGoods,{params:{uid,pid,pnum:1}}).then(function(res){
        showMycar();
        })
    }
    
    //展示购物车商品
    function showMycar(){
        var id=JSON.parse(localStorage.getItem("username"));
        var cargoodsnum=document.querySelector(".cargoodsnum");
        var goods_ul=document.querySelector(".goods_ul");
        var car_ab=document.querySelector(".car_ab");
        var quanxuan=document.querySelector(".quanxuan");
        var topay=document.querySelector(".topay");
        var html="";
        axios.get(ShowCar,{params:{id}}).then(function(res){
            var cargoods=res.data.data;
            //判断是否为空
            if(cargoods.length==0){
                car_ab.style.display="block";
                quanxuan.style.display="none";
                topay.style.display="none";
            }else{
                car_ab.style.display="none";
                quanxuan.style.display="block";
                topay.style.display="block";
            }
            cargoodsnum.innerHTML=cargoods.length;
            cargoods.forEach(function(v,i){
                html+=`<li class="goods_li">
                <input type="checkbox" name="" class="sbtn" onclick="soloBox(checked,${i})"/>
                <div class="li_img">
                  <img src="${v.pimg}" alt="" />
                </div>
                <div class="goods_div">
                  <p class="goods_div_name">${v.pname}</p>
                  <p>￥<span class="goodsjg">${v.pprice}</span></p>
                  <div class="liang">
                    <p>数量</p>
                    <span class="jian_" onclick="carJian(${i},${v.pid})">-</span><span class="goods_nums">${v.pnum}</span
                    ><span class="jia_" onclick="carJia(${i},${v.pid})">+</span>
                    <button class="goods_div_del" onclick="delCar(${v.pid})">删除</button>
                  </div>
                </div>
              </li>`
            })
            goods_ul.innerHTML=html;
            jsPrice();
         })
        //如果刷新页面 全选取消
        var allbox=document.querySelector(".quanxuan input");
        allbox.checked=false; 
    }
showMycar();

/* 点击数量按钮*/
function carJian(j,pid){
    var goods_nums=document.querySelectorAll(".goods_nums");
    var nums=parseInt(goods_nums[j].innerHTML);
    if(goods_nums[j].innerHTML<=1){
        nums=1;
    }else{
        nums--;
        }
    goods_nums[j].innerHTML=nums;
    var pnum=nums;
    axios.get(ChangenumCar,{params:{uid,pid,pnum}}).then(function(res){
        console.log(res.data);
    });
    jsPrice();
}
function carJia(j,pid){
    var uid=JSON.parse(localStorage.getItem("username"));
    var goods_nums=document.querySelectorAll(".goods_nums");
    var nums=parseInt(goods_nums[j].innerHTML);
    nums++;
    goods_nums[j].innerHTML=nums;
    var pnum=nums;
    axios.get(ChangenumCar,{params:{uid,pid,pnum}}).then(function(res){
        console.log(res.data);
    });
    jsPrice();
}
/* 删除购物车商品 */
function delCar(pid){
    var uid=JSON.parse(localStorage.getItem("username"));
    axios.get(DelCar,{params:{uid,pid}}).then(function(res){
        console.log(res.data);
        showMycar();
    })
}

/* 全选单选点击*/
//点击全选，
var allbox=document.querySelector(".quanxuan input");
allbox.checked=false;
allbox.onclick=function(){
    var sbtn=document.querySelectorAll(".sbtn");
    //计算价格
    if(this.checked==true){
        sbtn.forEach(function(v,i){
            v.checked=true;
        })
    }else{
        sbtn.forEach(function(v){
            v.checked=false;
        })
    }
    
    jsPrice();
}
//点击单选，单选全选，全选勾选
function soloBox(checked,i){
    var sbtn=document.querySelectorAll(".sbtn");
    if(!checked){
        allbox.checked=false;
    }else{
        allbox.checked=true;
        sbtn.forEach(function(v){
            if(!v.checked){
                allbox.checked=false;
            }
        })
    }   
    jsPrice(); 
}
//计算价格
function jsPrice(){
    var pricenum=document.querySelector(".topay span");
    var goodsjg=document.querySelectorAll(".goodsjg");
    var goods_nums=document.querySelectorAll(".goods_nums");
    var sbtn=document.querySelectorAll(".sbtn");
    var sum=0;
    sbtn.forEach(function(v,i){
        if(v.checked){
            sum+=goodsjg[i].innerHTML*goods_nums[i].innerHTML;
        }
    })
    pricenum.innerHTML=sum;

}
}else{
    function addgoods(){
        alert("请先登录");
    };
    function addOne(){
        alert("请先登录");
    }
     
}
/* 先点击列表的任意一个就跳转到全部商品页面 */
var navlist=document.querySelector(".navlist");
navlist.onclick=function(ev){
    var ev = ev || window.event;
　　var target = ev.target || ev.srcElement;
    if(target.nodeName.toLowerCase() == 'li'){
            //截取字符串
            var attr=target.innerHTML;
            attr=attr.split(">");
            localStorage.setItem("goodsname",attr[2]);
            location.href="../html/allgoods.html";
    　}

}

/* 去逛一逛 */
var delbtn=document.querySelector(".delbtn");
delbtn.onclick=function(){
    location.href="../html/allgoods.html";
}